package com.hqbzl.c1;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;

@Slf4j
public class TestSync1 {
    static int a = 0;
    static CountDownLatch cdt = new CountDownLatch(2);
    static Object object = new Object();

    public static void main(String[] args) throws InterruptedException {
        t1();
        t2();
        cdt.await();
        System.out.println("a = " + a);
    }

    public static void t1() {
        System.out.println("t1 start");
        new Thread(() -> {
            try {
                for (int i = 0; i < 5000; i++) {
                    synchronized (object) {
                        a++;
                    }
                }
            } finally {
                cdt.countDown();
            }
        }, "t1").start();
    }

    public static void t2() {
        System.out.println("t2 start");
        new Thread(() -> {
            try {
                for (int i = 0; i < 5000; i++) {
                    synchronized (object) {
                        a--;
                    }
                }
            } finally {
                cdt.countDown();
            }
        }, "t1").start();

    }
}
